package com.qc.common;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	/**
	 * 获取系统当前时间戳
	 * @return
	 */
	public static long getSysTimestamp() {
		return Calendar.getInstance().getTime().getTime();
	}
	
	/**
	 * 获取系统当前timestamp
	 */
	public static Timestamp getCurrentSysTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}
	
	/**
	 * 获取系统当前时间
	 * @return
	 */
	public static String getSysNowTime() {
		String retStr="";
		try{
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			retStr=df.format(Calendar.getInstance().getTime());
		}
		catch(Exception e){
//			Util.logError(e);
		}
		return retStr;
	}
	
	/**
	 * 离线用户初始化获取消息时间戳
	 * @return
	 */
	public static long getOffUserTimestamp() {
		Calendar cal=Calendar.getInstance(); 
		int currHour = Calendar.HOUR_OF_DAY;
		if (currHour <= 9) {
			cal.add(Calendar.HOUR_OF_DAY, -6);
		} else {
			cal.add(Calendar.HOUR_OF_DAY, -3);
		}
		return cal.getTime().getTime();
	}
	
	/**
	 * 获取当前时间后一天日期 yyyy-MM-dd
	 * @return
	 */
	public static String getAfterOneDay() {
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, 1);
		return dateFormatString(cal.getTime());
	}
	
	
	public static String getAfterOneDay2() {
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return dateFormatString(cal.getTime());
	}
	
	/**
	 * 获取当天聊天消息
	 * @return
	 */
	public static long getDayTimestamp() {
//		Date nowDay = stringFormatDate(getSysNowTime());
//		return nowDay.getTime();
		Calendar cal=Calendar.getInstance(); 
		cal.add(Calendar.HOUR, -2);
		return cal.getTime().getTime();
	}
	
	/**
	 * 获取当前时间yyyy-MM-dd hh:mm:ss
	 * @return
	 */
	public static String getSysTimeout(){
		String retStr="";
		try{
			Calendar cal=Calendar.getInstance(); 
			cal.add(Calendar.MINUTE, 10);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			retStr=df.format(cal.getTime());
		}
		catch(Exception e){
//			Util.logError(e);
		}
		return retStr;
	}
	
	/**
	 * 获取当前时间yyyy-MM-dd hh:mm:ss
	 * @return
	 */
	public static String getNowTime(){
		String retStr="";
		try{
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			retStr=df.format(new Date());
		}
		catch(Exception e){
//			Util.logError(e);
		}
		return retStr;
	}
	
	public static String getNowTimeUnFormat(){
		String retStr="";
		try{
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			retStr=df.format(new Date());
		}
		catch(Exception e){
//			Util.logError(e);
		}
		return retStr;
	}
	
	/**
	 * 字符串转为日期格式
	 * 
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static Date stringFormatDate(String dateString){
		try {
			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = bartDateFormat.parse(dateString);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date stringFormatDateAddDay(String dateString, int day) {
		try {
			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = bartDateFormat.parse(dateString);
			Calendar cal=Calendar.getInstance(); 
			cal.setTime(date);
			cal.add(Calendar.DATE,1);
			return cal.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date stringFormatDateHous(String dateString){
		try {
			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date date = bartDateFormat.parse(dateString);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 字符串转为日期格式
	 * 
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static Date stringFormatDateTime(String dateString) {
		try{
			if (dateString.length() < 11) {
				dateString += " 00:00:00";
			}
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date date = bartDateFormat.parse(dateString);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date stringFormatDateTime2(String dateString) {
		try{
			if (dateString.length() < 11) {
				dateString += " 00:00";
			}
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm");
			Date date = bartDateFormat.parse(dateString);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将时间格式化为含时分秒的字符串
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String dateTimeFormatString(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(date);
	}

	public static String dateFormatString(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

	/**
	 * 将时间格式化为含时分秒的字符串 24小时进制
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static String date24HourFormatString(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(date);
	}

	public static String greenwichMeanTime(Date date) {
		String strExpriseDate = date.toString();
		String[] strExpriseDates = strExpriseDate.split(" ");
		String stringDate = strExpriseDates[0] + ", " + strExpriseDates[2]
				+ " " + strExpriseDates[1] + " " + strExpriseDates[5] + " "
				+ strExpriseDates[3] + " GMT";
		return stringDate;
	}
	
	public static String dateFormatString(Date date,String str) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(str);
		return dateFormat.format(date);
	}
	
	public static Date dayToDate(Date date,int d){
		if (date == null) {
			date = new Date();
		}
		Calendar bDate = Calendar.getInstance();
		bDate.setTime(date);
		bDate.add(Calendar.DATE, d);
		return bDate.getTime();
	}
	/**
	 * 两个日期之间相隔多少天
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long diffInDate(Date d1, Date d2) {
		if(null == d1 || null == d2){
			return -1;
	    }
		String pattern = "yyyy-MM-dd";
		String d1_str = format(d1, pattern);
		String d2_str = format(d2, pattern);		
		d1 = parseDate(d1_str, pattern);
		d2 = parseDate(d2_str, pattern);
		long diff = (long) (Math.abs(d1.getTime() - d2.getTime())) / 86400000; 		
	    return diff;
	}
	
	/**
	 * 两个日期之间相隔多少天(含负数)
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long diffDate(Date d1, Date d2) {
		if(null == d1 || null == d2){
			return -1;
	    }
		String pattern = "yyyy-MM-dd";
		String d1_str = format(d1, pattern);
		String d2_str = format(d2, pattern);		
		d1 = parseDate(d1_str, pattern);
		d2 = parseDate(d2_str, pattern);
		long diff = (long) (d1.getTime() - d2.getTime()) / 86400000; 		
	    return diff;
	}
	
	
	/**
	 * 日期格式化
	 * @param date    日期
	 * @param pattern 格式化模式
	 * @return 格式化后的日期
	 */
	public static String format(Date date, String pattern) {
		SimpleDateFormat $format = new SimpleDateFormat(pattern);
		return $format.format(date);
	}
	
	public static Date parseDate(String dateString, String pattern) {
		SimpleDateFormat $format = new SimpleDateFormat(pattern);
		try {
			return $format.parse(dateString);
		} catch (Exception e) {
			dateString = format(new Date(), pattern);
			return parseDate(dateString, pattern);
		}
	}
	
	public static String timestampParseString(Timestamp timestamp) {
		if (timestamp == null) {
			return null;
		}
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(timestamp);
	}
	
	public static String timestampParseString2(String timestamp) {
		if (timestamp == null) {
			return null;
		}
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(timestamp);
	}
	
	/**
	 * 将 20170227150411 不带符号的日期字符串转换成带符号的日期字符串
	 * @param longDateStr
	 * @return
	 */
	public static String stringFormatString(String longDateStr) {
		SimpleDateFormat d = new SimpleDateFormat("yyyyMMddHHmmss");
		
		Date date1;
		try {
			date1 = d.parse(longDateStr);
			
			String dString = DateUtil.dateTimeFormatString(date1);
			return dString;
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 将2017-04-10 13:49:57 转成 20170410134957
	 * @param longDateStr
	 * @return
	 */
	public static String stringFormat(String longDateStr) {
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		try {
			Date date = sdf2.parse(longDateStr);
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
			String time = sdf1.format(date);
			return time;
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		return "";
	}
	
	
	
	/**
	 * 2个时间相差的小时
	 * @param endDate
	 * @param nowDate
	 * @return
	 */
	public static long getDatePoor(Date endDate, Date nowDate) {
		 
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();

	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff / nh;
	    return hour;
	}
	
	public static String getDatePoorDate(Date endDate, Date nowDate) {
		 
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    if (day == 0) {
	    	return hour + "小时";
	    }else {
	    	return day + "天" + hour + "小时";
	    }
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    
	}
	
	
	
	/**
	 * 2个时间相差的天数
	 * @param endDate
	 * @param nowDate
	 * @return
	 */
	public static long getDateDays(Date endDate, Date nowDate) {
		 
	    long nd = 1000 * 24 * 60 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();

	    // 计算差多少天
	    long day = diff / nd;
	    return day;
	}
	
	/**
	 * 获取当前时间的后一天
	 */
	public static String currentLaterOne() {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 1);
        return sf.format(c.getTime());
	}
	
	/**
	 * 获取当前时间减一天
	 * @return
	 */
	public static String currentLaterOne2() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        return sf.format(c.getTime());
    }
	
	public static String time(){
	    SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
	    String time = format2.format(Calendar.getInstance().getTime());
	    System.out.println("完整的日期： " + time);
	    return time;
	}
	
	//获取根据当前日期 每周星期一时间  排行榜
	public static String monday(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    
	    Calendar cal = Calendar.getInstance();

        cal.setTime(new Date());
		
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了  

        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天  

        if(1 == dayWeek) {

          cal.add(Calendar.DAY_OF_MONTH, -1);

        }  

        System.out.println("要计算日期为:"+sdf.format(cal.getTime())); //输出要计算日期  

        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一  

        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天  

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值   

        String imptimeBegin = sdf.format(cal.getTime()); //周一时间
        
        return imptimeBegin+" 00:00:00";
	}
	
	//获取每周一 日期
	public static String monday2(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		return sdf.format(c.getTime());
	}
	
	//获取下周一日期
	public static String monday3(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 8);
		return sdf.format(c.getTime());
	}
	
	//获取每周星期天
	public static String sunday(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 7);
		return sdf.format(c.getTime());
	}
	
	public static String sunday2(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 7);
		String time = sdf.format(c.getTime());
		
		return time + " 59:59:59";
	}
	
	
	public static void main(String[] args) throws ParseException {
		
		//System.out.println(DateUtil.monday());
		
		//System.out.println(DateUtil.sunday2());
		
		System.out.println(DateUtil.timestampParseString2("2017-05-09 11:45:15.0"));
	
        
	   //System.err.println(time());
	    
	   //System.out.println(getAfterOneDay2());
		
	   /*System.out.println(DateUtil.getDateDays(DateUtil.stringFormatDate("2017-04-04"), DateUtil.stringFormatDate("2017-03-04")));
	   
	   long time = System.currentTimeMillis();
       
       SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
       String time2 = sf.format(time);
       System.out.println(time2);*/
		/*int newStr1 = Integer.valueOf("2017-03-04".replaceAll("-", ""));
		System.out.println(newStr1);
		
		

		System.out.println("Now Timestamp == " + getSysTimestamp());
		System.out.println(new Date().getTime());
		
		System.out.println(getOffUserTimestamp());
		System.out.println(Calendar.getInstance().getTimeInMillis());
		
		System.out.println(DateUtil.getDatePoor(DateUtil.stringFormatDateTime("2017-03-04 00:00:00"), new Date()));*/
	}
}
